package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Teacher;
import com.itheima.edu.info.manager.service.TeacherService;

import java.util.Scanner;

public class TeacherController {

   private TeacherService service =new TeacherService();

    private Scanner sc =new Scanner(System.in);

    //开启教师管理系统，并展示教师管理系统菜单
    public void start() {
       while(true){
           System.out.println("--------欢迎来到 <老师> 管理系统--------");
           System.out.println("请输入您的选择: 1.添加老师  2.删除老师  3.修改老师  4.查看老师  5.退出");
           String choice =sc.next();
           switch (choice){
               case "1":
                  // System.out.println("添加");
                   addTeacher();
                   break;
               case "2":
                   //System.out.println("删除");
                   deleteTeacher();
                   break;
               case "3":
                  // System.out.println("修改");
                   updateTeacher();
                   break;
               case "4":
                    //System.out.println("查看");
                   findAllTeacher();
                   break;
               case "5":
                   System.out.println("感谢您使用老师管理系统，再见");
                   return;
               default:
                   System.out.println("您的输入有误，请重新输入");
                   break;
           }
       }

    }

    //修改老师
    public void updateTeacher() {
        System.out.println("请输入要修改的老师id:");
        String updateId = inputId(true);
        Teacher newTea = inputTeacherInfo(updateId);
        service.updateTeacher(updateId,newTea);
        System.out.println("修改成功!");
    }

    //删除老师
    public void deleteTeacher() {
        System.out.println("请输入要删除的教师id:");
        String delId = inputId(true);
        service.deleteTeacher(delId);
        System.out.println("删除成功!");
    }

    //查询老师信息
    public void findAllTeacher() {
        Teacher[] teas = service.findAllTeacher();
        if(teas ==null){
            System.out.println("暂无信息请添加后重试");
        }else {
            System.out.println("教师号\t\t姓名\t年龄\t生日");
            for (int i = 0; i < teas.length; i++) {
                Teacher tea =teas[i];
                if(tea !=null){
                    System.out.println(tea.getId() + "\t" + tea.getName() + "\t" + tea.getAge() + "\t\t" + tea.getBirthday());
                }
            }
        }
    }

    //添加老师
    public void addTeacher() {
        System.out.println("请输入教师号:");
        String id = inputId(false);
        Teacher tea = inputTeacherInfo(id);
        boolean res = service.addTeacher(tea);

        if(res){
            System.out.println("添加成功");
        }else{
            System.out.println("添加失败");
        }
    }

    public String inputId(boolean flag){
        String id;
        while (true) {
            id = sc.next();
            boolean exists = service.isExists(id);
            if (exists ==  flag) {
                break;
            } else {
                System.out.println("当前id不可用,请重新输入!");
            }
        }
        return id;
    }

    public Teacher inputTeacherInfo(String id){
        System.out.println("请输入姓名:");
        String name = sc.next();
        System.out.println("请输入年龄:");
        String age = sc.next();
        System.out.println("请输入生日:");
        String birthday = sc.next();

        Teacher tea = new Teacher();
        tea.setId(id);
        tea.setName(name);
        tea.setAge(age);
        tea.setBirthday(birthday);

        return tea;
    }
}
